package dat255.calendarclock.controller;

import java.util.GregorianCalendar;
import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import dat255.calendarclock.R;
import dat255.calendarclock.controller.event.ShowEventController;
import dat255.calendarclock.model.event.EventHandler;
import dat255.calendarclock.model.event.SubEvent;
import dat255.calendarclock.view.ListCalendarView;

/**
 * An activity that is showing events using a list
 * @author Marcus Hultman
 */
public class ListCalendarController extends Activity implements Observer{	
	
	//Field
	private static ListCalendarView listCalendarView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		EventHandler.getInstance().addObserver(this);
		setContentView(R.layout.listview_layout);
		listCalendarView = (ListCalendarView) findViewById(R.id.listCalendarView);
	}

	@Override
	protected void onResume() {
		update(null, null);
		super.onResume();
	}
	
	@Override
	public void update(Observable observable, Object data) {
		listCalendarView.loadEvents(GregorianCalendar.YEAR);//Scroll to next event from the selected event.
	}
	/**
	 * when an item is selected the show view is opened
	 */
	public void listItemClicked(View view){
		try{
		SubEvent subEvent= (SubEvent)listCalendarView.getItemAtPosition(listCalendarView.getPositionForView(view));
		EventHandler.getInstance().setSelectedEvent(subEvent);
		startActivity(new Intent(this,ShowEventController.class));
		}catch (Exception e) {
			Log.d("debug",""+(e.getClass().getName()));
		}
	}
}
